package edu.udel.libra.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import edu.udel.libra.model.FantasyTeam;
import edu.udel.libra.model.League;
import edu.udel.libra.model.Player;
import edu.udel.libra.model.User;
import edu.udel.libra.model.repository.FantasyTeamRepository;
import edu.udel.libra.model.repository.LeagueRepository;

@Controller
@RequestMapping("/fantasy-league")
public class LeagueController extends AbstractController
{
	private static final Logger LOG = Logger.getLogger(LeagueController.class.getName());
	
	private static final String LIST = "league_list";
	private static final String LEAGUE = "league";
	private static final String PLAYERS = "players";
	private static final String TEAM = "team";
	
	@Autowired
	private LeagueRepository leagueRepository;
	
	@Autowired
	private FantasyTeamRepository fantasyTeamRepository;
	
	/* League List view */
	@RequestMapping("")
	public String leagueList(Model model) {
		LOG.info("starting leagueList");
		
		User user = getUser();
		
		List<League> leagues = leagueRepository.getLeaguesForUser(user.getId());
    
		model.addAttribute("user", user);
		model.addAttribute("leagues", leagues);
		return LIST;
	}
	
	/* League Info view */
	@RequestMapping(value = "/{leagueId}", method = RequestMethod.GET)
	public String league(Model model, @PathVariable("leagueId") Integer leagueId) {	
		LOG.info("starting league GET");
		
		User user = getUser();
		
		League league = leagueRepository.getLeague(leagueId);
	
		model.addAttribute("user", user);
		model.addAttribute("league", league);
		return LEAGUE;
	}
	
	/* Team Info view */
	@RequestMapping(value = "/{leagueId}/{teamId}", method = RequestMethod.GET)
	public String team(Model model, @PathVariable("leagueId") Integer leagueId, @PathVariable("teamId") Integer teamId) {	
		LOG.info("starting team GET");
		
		User user = getUser();
		
		League league = leagueRepository.getLeague(leagueId);
		FantasyTeam fantasyTeam = fantasyTeamRepository.getTeam(teamId, leagueId);
	
		model.addAttribute("user", user);
		model.addAttribute("fantasyTeam", fantasyTeam);
		model.addAttribute("league", league);
		return TEAM;
	}
	
	/* League Player List view */
	@RequestMapping(value = "/{leagueId}/players", method = RequestMethod.GET)
	public String leaguePlayerList(Model model, @PathVariable("leagueId") Integer leagueId) {
		LOG.info("starting leaguePlayerList GET");
		
		User user = getUser();
		
		League league = leagueRepository.getLeague(leagueId);
		List<Player> players = league.getPlayers();
    
		model.addAttribute("user", user);
		model.addAttribute("players", players);
		model.addAttribute("league", league);
		return PLAYERS;
	}

}
